/**
 * 
 * @param {*} name api名称
 * @param {*} config 参数
 * @returns 
 */
const _promise = (name, config) => {
	return new Promise((resolve, reject) => {
		Object.assign(config, {
			success: (res) => {
				resolve(Object.assign(res))
			},
			fail: (err) => {
				reject(Object.assign(err))
			},
		})
		uni[name](config)
	})
}

/**
 * 
 * @param {*} linkType 跳转类型
 * @param {*} params 跳转参数
 * @returns 
 */
const _linkTo = (
	linkType,
	params = {}
) => {
	const _params = {
		url: "",
		args: JSON.parse(JSON.stringify(params))
	}
	if (typeof params === "string") {
		_params.url = params
	} else if (params?.url) {
		_params.url = params = params?.url || ""
		delete _params.args.url
		delete _params.args.success
		delete _params.args.fail
		delete _params.args.complete
	}
	return _promise(linkType, {
		url: _params.url,
		..._params.args
	})
}

/**
 * 
 * @param {*} args 
 * @returns 
 * 跳转方式promise华
 */
export const navigateTo = (args) => _linkTo("navigateTo", args)
export const switchTab = (args) => _linkTo("switchTab", args)
export const redirectTo = (args) => _linkTo("redirectTo", args)
export const reLaunch = (args) => _linkTo("reLaunch", args)
export const navigateBack = (args = {}) => _linkTo("navigateBack", args)

export const showToast = (params) => _promise("showToast", params)
export const showModal = (params) => _promise("showModal", params)
// showModal用法
/*
// 显示消息提示框
showToast({
	title: "操作成功",
	icon: "success",
	duration: 2000
});

// 显示模态对话框
showModal({
	title: "提示",
	content: "确定要删除吗？",
	showCancel: true
}).then(res => {
	if (res.confirm) {
		console.log("用户点击了确定");
	} else if (res.cancel) {
		console.log("用户点击了取消");
	}
});
*/

export const getProvider = (params) => _promise("getProvider", params)
export const uniLogin = (params = {}) => _promise("login", params)
export const showLoading = (params = {}) => _promise("showLoading", params)
export const getLocation = (params = {}) => _promise("getLocation", params)
export const chooseImage = (params = {}) => _promise("chooseImage", params)
export const chooseFile = (params = {}) => _promise("chooseFile", params)
export const uploadFile = (params) => _promise("uploadFile", params)
export const openLocation = (params) => _promise("openLocation", params)
export const chooseLocation = (params = {}) => _promise("chooseLocation", params)
export const downloadFile = (params) => _promise("downloadFile", params)
